<template>
    <van-address-edit 
        show-set-default
        :area-list="areaList"
        :area-columns-placeholder="['请选择', '请选择', '请选择']"
        :address-info="address"
        @save="save"
    />
</template>

<script setup>
    // 引入地区数据
    import { areaList } from '@vant/area-data';

    const { proxy } = getCurrentInstance();

    // 初始数据
    const address = ref({});

    // 用户信息
    const business = proxy.$business;

    // 初始化数据
    onBeforeMount(() => address.value = {
        name: business.value.nickname,
        tel: business.value.mobile,
        areaCode: business.value.district || business.value.city || business.value.province,
    })

    // 添加
    const save = async (values) =>
    {
        // 组装数据
        let params = {
            busid: business.value.id,
            consignee: values.name,
            address: values.addressDetail,
            mobile: values.tel,
            status: values.isDefault ? 1 : 0,
            code: values.areaCode
        }

        // 发送请求
        let result = await proxy.$POST({
            url: "/address/add",
            params
        })

        if(result.code === 1)
        {
            proxy.$success({
                message: result.msg,
                onClose: () =>{
                    proxy.$router.back();
                }
            })
        }else
        {
            proxy.$fail(result.msg);
        }
    }
</script>